package com.wsk.dao.Impl;

import com.wsk.bean2.UserInformation;
import com.wsk.dao.LoginDAO;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public class LoginDAOImpl<T> implements LoginDAO{
    @Autowired
    private SessionFactory sessionFactory;
    private Session session;
    // 获取id
    public int getId(String sql) {
        int id = 0;
        try {
            System.out.println(new Date());
            System.out.println("run getId()");
            session = getSession();
            List<Integer> list = session.createSQLQuery(sql).list();
            if (!list.isEmpty()) {
                Iterator iterator = list.iterator();
                id = (int) iterator.next();
                System.out.println(id);
                System.out.println("run getId() success\n");
                return id;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }

    // 判断是否登录成功
    public boolean Login(String sql) {
//		lock.lock();
        try {
            System.out.println(new Date());
            System.out.println("run login()");
            session = this.getSession();
            List list = session.createSQLQuery(sql).list();
            if (!list.isEmpty()) {
                Iterator iterator = list.iterator();
                String getString = (String) iterator.next();
                if (getString != null || getString != "") {
                    System.out.println("phone is not empty in login()");
                    System.out.println("run login() success\n");
                    return true;
                }
            }
            System.out.println("run login() success\n");
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除 SQLQuery
    public boolean deleteSQL(String sql) {
        try {
            System.out.println(new Date());
            System.out.println("deleteSQL");
            session = this.getSession();
            session.createSQLQuery(sql).executeUpdate();
            System.out.println("deleteSQL success\n");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 账号是否为空
    public boolean isNull(String sql) {
        try {
            System.out.println(new Date());
            System.out.println("run isNull()");
            session = this.getSession();
            List list = session.createSQLQuery(sql).list();
            if (!list.isEmpty()) {
                Iterator iterator = list.iterator();
                String getString = (String) iterator.next();
                if (getString != null || getString != "") {
                    System.out.println("phone is not null in isNull()");
                    System.out.println("run isNull() success\n");
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    // 更改密码
    public  boolean updatePassword(Object t) {
        try {
            System.out.println(new Date());
            System.out.println("updatePassword");
            session = this.getSession();
            session.update(t);
            session.flush();
            System.out.println("run updatePassword() success\n");
            return true;
        } catch (Exception e) {

            e.printStackTrace();
            return false;
        }
    }

    // 保存用户头像
    public  boolean saveImage(T t) {
        try {
            System.out.println(new Date());
            System.out.println("run saveImage()");
            session = this.getSession();
            session.saveOrUpdate(t);
            System.out.println("run saveImage() success\n");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 先判断要保存的头像是否为空，然后更新头像
    public boolean ImageisEmpty(String sql) {
        try {
            System.out.println(new Date());
            System.out.println("run ImageisEmpty()");
            session = this.getSession();
            List list = session.createSQLQuery(sql).list();
            if (!list.isEmpty()) {
                Iterator iterator = list.iterator();
                String string = (String) iterator.next();
                if (string!="http://119.29.194.92:8080/movie/default/headpicture/wuqiong.jpg") {
                    System.out.println("phone is not empty in ImageisEmpty()");
                    System.out.println("run ImageisEmpty() suceess\n");
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    // 获取头像地址
    public String getPictureURL(String sql) {
        System.out.println(new Date());
        System.out.println("run getPictureURL()");
        String url = "";
        try {
            session = this.getSession();
            List<String> list = session.createSQLQuery(sql).list();
            if (!list.isEmpty()) {
                Iterator<String> iterator = list.iterator();
                url = iterator.next();
                System.out.println(url);
                System.out.println("run getURL() success\n");
                return url;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return url;
        }
        return url;
    }

    // 保存
    public boolean save(Object t) {
        System.out.println(new Date());
        System.out.println("run save()");
        try {
            session = getSession();
            session.save(t);
            session.flush();
            System.out.println("save success");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public UserInformation getUserInformation(int id,String sql) {
        System.out.println(new Date());
        System.out.println("getUserInformation");
//        List<UserInformation> information=new ArrayList<>();
        UserInformation userInformation=new UserInformation();
        try {
            session = this.getSession();
            List<Object[]> list2 = session.createSQLQuery(sql).setParameter(0,id).list();
            int i = 0;
            for (Object[] row : list2) {
                userInformation.setId(Integer.parseInt(row[i++].toString()));
                userInformation.setPhone(row[i++].toString());
                if (row[i] == null){
                    i++;
                    userInformation.setAddress("");
                }
                else
                    userInformation.setAddress(row[i++].toString());
                if (row[i] == null){
                    i++;
                    userInformation.setBirthday("");
                }
                else
                    userInformation.setBirthday(row[i++].toString());
                if (row[i] == null){
                    i++;
                    userInformation.setSex("");
                }
                else
                    userInformation.setSex(row[i++].toString());
                if (row[i] == null){
                    i++;
                    userInformation.setAutograph("");
                }
                else
                    userInformation.setAutograph(row[i++].toString());
                if (row[i] == null){
                    i++;
                    userInformation.setLabel("");
                }
                else
                    userInformation.setLabel(row[i++].toString());
                userInformation.setBuildtime(row[i++].toString());
                userInformation.setName(row[i++].toString());
                userInformation.setLevel(Double.parseDouble(row[i++].toString()));
                userInformation.setIntegral(Integer.parseInt(row[i++].toString()));
//                information.add(userInformation);
                // list.add(map);
                return userInformation;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return userInformation;
        }
        return userInformation;
    }
    public UserInformation getUserInformation(String sql){
        System.out.println(new Date());
        System.out.println("getUserInformation");
        UserInformation userInformation=new UserInformation();
        try {
//            getSession().createSQLQuery(sql).setParameter(0).list();
        } catch (Exception e) {
            e.printStackTrace();
            return userInformation;
        }
        return userInformation;
    }


    // 更新 单独信息
    public boolean update(String sql){
        try {
            System.out.println(new Date());
            System.out.println("update");
            session = getSession();
            session.createSQLQuery(sql).executeUpdate();
            System.out.println("update success\n");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("update fail\n");
            return false;
        }
    }

    //查看今天是否签到
    public boolean getCheckIn(String sql) {
        try {
            System.out.println(new Date());
            System.out.println("getCheckIn");
            session=getSession();
            List<Integer> list = session.createSQLQuery(sql).list();
            if (!list.isEmpty()) {
                Iterator<Integer> it=list.iterator();
                int id=it.next();
                System.out.println(id);
                if (id!=0)
                    return true;
                else
                    return false;
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return false;
    }

    // 获取积分来源
    public Map<String, String> getIntegralSource(String sql) {
//		lock.lock();
        System.out.println(new Date());
        System.out.println("get integralSource");
        Map<String, String> map = new HashMap<>();
        try {
            session = this.getSession();
            List<Object[]> list = session.createSQLQuery(sql).list();
            int i = 0;
            for (Object[] row : list) {
                map.put("id", row[i++] + "");
                map.put("method", row[i++].toString());
                map.put("time", row[i++].toString());
                map.put("iid", row[i++] + "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        return map;
    }

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }
}
